package com.tjcd.project.library_base.common.upload.net;

import com.blankj.utilcode.util.LogUtils;
import com.tjcd.project.library_base.base.bean.BaseResponseBean;
import com.tjcd.project.library_base.common.upload.net.interf.IBaseNet;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.Scheduler;

/**
 * @Author junweiliu
 * @Description Bean网络单元
 * @Version 1.0
 * @CreateDate 2019/12/24
 * @QQ 1007271386
 */
@SuppressWarnings("ALL")
public class BeanNetUnit<T extends BaseResponseBean> implements IBaseNet {
    /**
     * 网络句柄
     */
    protected Observable mObservable;
    /**
     * 回调
     */
    protected BaseFileObserver<T> mObserver;

    @Override
    public IBaseNet setObservable(Observable observable) {
//        setObservable(observable, Schedulers.io(), AndroidSchedulers.mainThread());
        return this;
    }

    public IBaseNet setObservable(Observable observable, Scheduler subscribeon, Scheduler observeon) {
        mObservable = observable.subscribeOn(subscribeon).observeOn(observeon);
        return this;
    }

    @Override
    public IBaseNet request(Observer observer) {
        if (null == mObservable) {
            LogUtils.e("请通过setObservable设置被观察者(请求连接)");
            return this;
        }
        mObserver = (BaseFileObserver<T>) observer;
        mObservable.subscribe(mObserver);
        return this;
    }

    public Observable getObservable() {
        return mObservable;
    }

    @Override
    public void cancelRequest() {
        if (null == mObservable) {
            LogUtils.e("请通过setObservable设置被观察者(请求连接)");
            return;
        }
        mObserver.dispose();
    }
}
